package com.CloudDream.Listener;

import com.CloudDream.Gui.MYInventoryHolder;
import com.CloudDream.Gui.gui.CookGui;
import com.CloudDream.Gui.gui.RecipeGui;
import com.CloudDream.MoonCake;
import com.handy.lib.annotation.HandyListener;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;

@HandyListener
public class RecipeGuiEvent implements Listener {

    public MoonCake instance = MoonCake.getInstance();
    private RecipeGui inv;

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {

        Inventory inv = e.getInventory();

        if (e.getView().getTitle() != null && !e.getView().getTitle().equalsIgnoreCase("§bMoonCake §7- §a月饼合成展示")) {
            return;
        }
        if (!(inv.getHolder() instanceof MYInventoryHolder)) {
            return;
        }
        if (((MYInventoryHolder) inv.getHolder()).getType() != 1) {
            return;
        }
        int slot = e.getRawSlot();
        if (e.getClick().equals(ClickType.DOUBLE_CLICK)) {
            if (slot > 54 && slot < 90) {
                e.setCancelled(true);
                return;
            }
        }
        if (slot >= 0 && slot <= 54) {
            e.setCancelled(true);
        }

        if(slot == 23){
            e.getWhoClicked().closeInventory();
            e.getWhoClicked().openInventory(CookGui.open((Player)e.getWhoClicked()));
        }
        if(slot == 49){
            e.getWhoClicked().closeInventory();
        }
    }
}
